<template><div><h2 id="email-认证" tabindex="-1"><a class="header-anchor" href="#email-认证"><span>Email 认证</span></a></h2>
<p>Laravel 6 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="email-认证-1" tabindex="-1"><a class="header-anchor" href="#email-认证-1"><span>Email 认证</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#verification-database">数据库注意事项</a></li>
<li><a href="#verification-routing">路由</a>
<ul>
<li><a href="#protecting-routes">保护路由</a></li>
</ul>
</li>
<li><a href="#verification-views">视图</a></li>
<li><a href="#after-verifying-emails">Email认证之后</a></li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>很多 Web 应用会要求用户在使用之前进行 Email 地址验证。Laravel 不会强迫你在每个应用中重复实现它，而是提供了便捷的方法来发送和校验电子邮件的验证请求。</p>
<h3 id="model-准备" tabindex="-1"><a class="header-anchor" href="#model-准备"><span>Model 准备</span></a></h3>
<p>在开始之前，需要验证你的 <code v-pre>App\User</code> 模型是否实现了 <code v-pre>Illuminate\Contracts\Auth\MustVerifyEmail</code>契约：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>MustVerifyEmail</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span> <span class="token keyword">implements</span> <span class="token class-name">MustVerifyEmail</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据库注意事项" tabindex="-1"><a class="header-anchor" href="#数据库注意事项"><span>数据库注意事项</span></a></h2>
<h4 id="email-验证字段" tabindex="-1"><a class="header-anchor" href="#email-验证字段"><span>Email 验证字段</span></a></h4>
<p>接下来，你的 <code v-pre>user</code> 表必须包含一个 <code v-pre>email_verified_at</code> 字段用来存储 Email 地址通过验证的时间。默认情况下，Laravel 框架中 <code v-pre>users</code> 表的数据迁移已经包含了这个字段。所以，您需要做的就只是执行数据库迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h2>
<p>Laravel 的 <code v-pre>Auth\VerificationController</code> 类包含了发送验证链接和验证 Email 的必要逻辑。通过将 <code v-pre>verify</code> 选项传给 <code v-pre>Auth::routes</code> 方法，就能为这个控制器注册所需要的路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">routes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'verify'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="保护路由" tabindex="-1"><a class="header-anchor" href="#保护路由"><span>保护路由</span></a></h3>
<p><a href="https://learnku.com/docs/laravel/6.x/middleware" target="_blank" rel="noopener noreferrer">路由中间件</a> 可用于仅允许经过验证的用户访问指定路由。Laravel 附带了 <code v-pre>verified</code> 中间件，它定义在 <code v-pre>Illuminate\Auth\Middleware\EnsureEmailIsVerified</code>。由于此中间件已在应用程序的 HTTP 内核中注册，因此您需要做的就是将中间件附加到路由定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 只有经过验证的用户才能进..</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'verified'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="视图" tabindex="-1"><a class="header-anchor" href="#视图"><span>视图</span></a></h2>
<p>如果要生成Email验证的所有必要视图，您可以使用<code v-pre>laravel / ui</code> Composer包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>ui</span>
<span class="line"></span>
<span class="line">php artisan ui vue <span class="token operator">--</span>auth</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Email验证视图文件位于<code v-pre>resources / views / auth / verify.blade.php</code>中。 你可以根据自己的应用自由地调整这些视图的样式。</p>
<h2 id="邮箱认证之后" tabindex="-1"><a class="header-anchor" href="#邮箱认证之后"><span>邮箱认证之后</span></a></h2>
<p>在邮箱认证之后，用户会自动被重定向至 <code v-pre>/home</code>。你可通过在 <code v-pre>VerificationController</code> 中定义一个 <code v-pre>redirectTo</code> 方法或者属性来调整认证之后的跳转位置。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirectTo</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>Laravel在Email验证过程中发送<a href="https://learnku.com/docs/laravel/6.x/events" target="_blank" rel="noopener noreferrer">事件</a>。 你应该在 EventServiceProvider 中注册监听者：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 应用程序的事件监听器</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Illuminate\Auth\Events\Verified'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'App\Listeners\LogVerifiedUser'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/verification/5154" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/ver...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/verification/5154" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/ver...</a></p>
</blockquote>
</div></template>


